package cn.com.self.apis.controller;


import cn.com.self.apis.core.vo.ResultModel;
import cn.com.self.apis.member.entity.UTalentRecommendation;
import cn.com.self.apis.member.entity.UTalentRecommendationVO;
import cn.com.self.apis.member.service.ITalentRecommendationService;
import cn.com.self.apis.portal.entity.dto.RequirementsDTO;
import cn.com.self.apis.portal.entity.dto.RequirementsDTO2;
import cn.com.self.apis.portal.service.intf.IRequirementsService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author1 ssh
 * @author2
 * @date 2021-04-28
 */
@Controller
@Api(tags = "需求大厅")
public class DemandHallController {

    @Autowired
    private ITalentRecommendationService talentRecommendationService;
    @Autowired
    IRequirementsService requirementsService;

//    /**
//     * 新增推荐
//     *
//     * @return
//     * @Description //TODO
//     **/
//    @RequestMapping(value = "/talentRecommendation/create", method = RequestMethod.POST)
//    @ResponseBody
//    @ApiOperation(value = "新增推荐信息")
//    public ResponseEntity create(@RequestBody @Valid UTalentRecommendation entity, HttpServletRequest request) {
//        entity.setCreateTime(new Date());
//        entity.setStartTime(new Date());
//        UTalentRecommendation create = talentRecommendationService.create(entity);
//        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(create));
//    }


    /**
     * 分页获取推荐列表
     *
     * @return
     * @Description //TODO
     **/
    @ApiOperation(value = "分页获取推荐列表")
    @RequestMapping(value = "/talentRecommendation/page", method = RequestMethod.GET)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "page", value = "页码", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "size", value = "一页显示条数", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "jsyy", value = "技术语言，传id值", required = false, dataType = "String")
    })
    public ResponseEntity getTalentRecommendationPage(HttpServletRequest request, Pageable page){
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        ICustomPage<UTalentRecommendationVO> pageData = this.talentRecommendationService.getTalentRecommendationPage(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(pageData)));

    }

    /**
     * 获取推荐信息
     *
     * @return
     * @Description //TODO
     **/
    @ApiOperation(value = "获取推荐信息")
    @RequestMapping(value = {"/talentRecommendation/load/{id}"}, method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity loadEscrowInfo(@PathVariable("id") String id, HttpServletRequest request) {
        UTalentRecommendation talentRecommendation = this.talentRecommendationService.load(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(talentRecommendation)));

    }


//    /**
//     * 修改推荐信息
//     *
//     * @return
//     * @Description //TODO
//     **/
//    @ApiOperation(value = "修改推荐信息")
//    @RequestMapping(value = {"/talentRecommendation/update/{id}"}, method = RequestMethod.POST)
//    @ResponseBody
//    public ResponseEntity update(@PathVariable("id") String id, @RequestBody UTalentRecommendation entity, HttpServletRequest request) {
//        UTalentRecommendation talentRecommendation = this.talentRecommendationService.load(id);
//        CopyUtils.copyProperties(entity, talentRecommendation);
//        talentRecommendation.setUpdateTime(new Date());
//        UTalentRecommendation update = this.talentRecommendationService.update(talentRecommendation);
//        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(update)));
//
//    }
//
//    /**
//     * 删除推荐信息
//     *
//     * @return
//     * @Description //TODO
//     **/
//    @ApiOperation(value = "删除推荐信息")
//    @ResponseBody
//    @RequestMapping(value = "/talentRecommendation/delete/{id}", method = RequestMethod.DELETE)
//    public ResponseEntity deletes(@PathVariable("id") String id) {
//        this.talentRecommendationService.delete(id);
//        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
//    }


    @PostMapping(value = "/requirements/create")
    @ApiOperation(value = "创建需求发布")
    public ResponseEntity create(@RequestBody @Valid RequirementsDTO requestBody) {
        return requirementsService.createRequirements(requestBody);
    }
    @PostMapping(value = "/api/v1/requirements/update/{id}")
    @ApiOperation(value = "更新需求发布")
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody @Valid RequirementsDTO requestBody) {
        return requirementsService.updateRequirements(id,requestBody);
    }

    @DeleteMapping(value = "/api/v1/requirements/delete/{id}")
    @ApiOperation(value = "删除需求发布")
    public ResponseEntity deletesRequirements(@PathVariable("id") String id) {
        return requirementsService.deletesRequirements(id);
    }


    @GetMapping(value = "/api/v1/requirements/page")
    @ApiOperation(value = "分页获取需求发布列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "provincesCodes", value = "发布人所在省份字典编码集合：5,8,6",paramType = "query",required = false)  ,
            @ApiImplicitParam(name = "classificationIds", value = "行业分类id集合：1348887637934870530,1348887637934870535",paramType = "query",required = false)  ,
            @ApiImplicitParam(name = "requirementsTypeCodes", value = "需求类型字典编码集合：373,374",paramType = "query",required = false) ,
            @ApiImplicitParam(name = "minAmount", value = "最小金额：整数",paramType = "query",required = false)  ,
            @ApiImplicitParam(name = "maxAmount", value = "最大：整数",paramType = "query",required = false)  ,
            @ApiImplicitParam(name = "requirementsStateCodes", value = "项目状态字典编码集合：430,431",paramType = "query",required = false) ,
            @ApiImplicitParam(name = "sort", value = "排序格式：start_time desc(desc降序，asc升序)(字段时间start_time,金额budget_amount,周期development_cycle)",paramType = "query",required = false) ,
            @ApiImplicitParam(name = "nameOrtelephone", value = "关键字查询：项目名称模糊查询、手机号完整查询",paramType = "query",required = false) ,
            @ApiImplicitParam(name = "size", value = "分页大小：默认20",paramType = "query",required = false) ,
            @ApiImplicitParam(name = "page", value = "页码：默认1",paramType = "query",required = false)
    })
    public ResponseEntity getRequirementsPage(HttpServletRequest request, Pageable page ) {
        try {
            Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
            ICustomPage<RequirementsDTO2> pageData = this.requirementsService.queryRequirementsEntityList(queryParams, page);
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(pageData)));
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("error", e);
            System.out.println(e);
            return ResponseEntity.status(HttpStatus.OK).body(result);
        }


    }

    @GetMapping(value = {"/api/v1/requirements/load/{id}"})
    @ApiOperation(value = "根据id获取需求发布")
    public ResponseEntity getRequirementsById(@PathVariable("id") String id) {
        try {
            Map<String, Object> queryParams = new HashMap<>();
            queryParams.put("id", id);
            RequirementsDTO2 requirementsDTO = requirementsService.getRequirementsById2(queryParams);
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(requirementsDTO)));
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("error", e.toString());
            return ResponseEntity.status(HttpStatus.OK).body(result);
        }
    }

    @GetMapping(value = {"/api/v1/requirements/today/count"})
    @ApiOperation(value = "获取单日发布数量")
    public ResponseEntity getRequirementsCountByDate() {
        try {
            return requirementsService.getRequirementsCountByDate();
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("error", e);
            return ResponseEntity.status(HttpStatus.OK).body(result);
        }
    }


}

